﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.StsAdmin;
using SoftVenture.Sharepoint.Permissions;

namespace SoftVenture.Sharepoint.STSAdm.Commands.Permissions
{
    class List : ISPStsadmCommand
    {
        #region ISPStsadmCommand Members

        public string GetHelpMessage(string command)
        {
            string retMsg = System.Environment.NewLine + System.Environment.NewLine;
            retMsg += "Allows to manage list and document list permissions" + System.Environment.NewLine;
            retMsg += System.Environment.NewLine;
            retMsg += "Parameters:";
            retMsg += "-url <url>" + System.Environment.NewLine;
            retMsg += "\t-listName <list name>" + System.Environment.NewLine;
            retMsg += "\t-operation <inheritance | add | remove | removeAll>" + System.Environment.NewLine; 
            retMsg += "\t\t-account <user | group>" + System.Environment.NewLine;
            retMsg += "\t\t-role <role name>" + System.Environment.NewLine;
            retMsg += "\t\t[-BreakRoleInheritance <copyRole | noCopyRole>]" + System.Environment.NewLine;
            retMsg += "\t\t[-ResetRoleInheritance]" + System.Environment.NewLine;
            retMsg += System.Environment.NewLine + System.Environment.NewLine + System.Environment.NewLine;
            retMsg += "Copyright 2010 SoftVenture SA" + System.Environment.NewLine;
            retMsg += "  > For more information on this command and others:" + System.Environment.NewLine;
            retMsg += "  > http://www.softventure.pt/" + System.Environment.NewLine;
            retMsg += "  > Use of this command is at your own risk." + System.Environment.NewLine;
            retMsg += "  > Please, send your problems and comments to product_support@softventure.pt" + System.Environment.NewLine;
            retMsg += "  > SoftVenture SA assumes no liability." + System.Environment.NewLine;
            
            return retMsg;
        }

        public int Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
        {
            output = string.Empty;
            
            command = command.ToLowerInvariant();

            #region Validate Input

            if (!keyValues.ContainsKey("url"))
            {
                throw new InvalidOperationException("The url parameter was not specified.");
            }

            string url = keyValues["url"];

            if (!keyValues.ContainsKey("listName"))
            {
                throw new InvalidOperationException("The listName parameter was not specified.");
            }

            string listName = keyValues["listName"];

            if (!keyValues.ContainsKey("account"))
            {
                throw new InvalidOperationException("The account parameter was not specified.");
            }

            string account = keyValues["account"];

            if (!keyValues.ContainsKey("operation"))
            {
                throw new InvalidOperationException("The operation parameter was not specified.");
            }

            string operation = keyValues["operation"];

            if (!keyValues.ContainsKey("role"))
            {
                throw new InvalidOperationException("The role parameter was not specified.");
            }

            string role = keyValues["role"];

            bool breakRoleInheritance = keyValues.ContainsKey("BreakRoleInheritance");
            bool resetRoleInheritance = keyValues.ContainsKey("ResetRoleInheritance");
            #endregion

            ListHelper lh = new ListHelper(url, listName);


            switch (operation.ToLowerInvariant())
            {
                case "add":
                    /// Chamar a biblioteca de permissionamento da lista
                    lh.Add(role, account, ref output);

                    break;
                case "remove":
                    /// Chamar a biblioteca de permissionamento do item da lista
                    /// 
                    lh.Remove(role, account, ref output);

                    break;
                case "removeall":
                    /// Chamar a biblioteca de permissionamento do item da lista
                    /// 
                    lh.RemoveAll(account, ref output);

                    break;
                case "inheritance":
                    if (breakRoleInheritance)
                        switch (keyValues["BreakRoleInheritance"].ToLowerInvariant())
                        {
                            case "copyrole":
                                lh.BreakRoleInheritance(true, ref output);
                                break;
                            case "nocopyrole":
                                lh.BreakRoleInheritance(false, ref output);
                                break;
                            default:
                                throw new InvalidOperationException("Invalid BreakRoleInheritance value.");
                        }
                    if (resetRoleInheritance)
                        lh.ResetRoleInheritance(ref output);

                    break;
                default:
                    throw new InvalidOperationException("Invalid operation value.");
            }
            lh.Dispose();
            return 0;
        }

        #endregion
    }
}
